﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    view = ui->graphicsView;
    view->setScene(scene);

}

MainWindow::~MainWindow()
{
    delete ui;
}

//初始化界面
void MainWindow::initUi()
{

    //设置画面
    qDebug() << view->width();
    qDebug() << view->height();

    int width  = maxWidth*gridlen + 20;
    int height = maxHeight*gridlen + 20;

    view->setGeometry(0,0,width+100,height+100);
    mRect = QRect(-10,-10,width,height);
    scene->setSceneRect(mRect);
    QGraphicsRectItem* rect = new QGraphicsRectItem(mRect);
    scene->addItem(rect);

    for (int col=0;col<maxWidth;col++) {
        QVector<MyGrid*> tab;
        for (int row=0;row<maxHeight;row++) {
            MyGrid* grid = new MyGrid;
            grid->setPos(col*gridlen,row*gridlen);
            scene->addItem(grid);
            tab.append(grid);
        }
        gridTab.append(tab);
    }
}

//添加蛇
void MainWindow::addSnack(int x, int y)
{
    if((x<maxWidth)&&(x>=0)&&(y<maxHeight)&&(y>=0)){
        snakePoi.append(QPoint(x,y));
        gridTab[x][y]->setState(2);
    }
}

void MainWindow::moveSnake()
{
    if(snakePoi.count() < 0)
        return;
    if(snakeDir == 0)
        return;

    if(snakeNextDir>0)
        snakeDir = snakeNextDir;

    QPoint poi = snakePoi.at(0);
    switch (snakeDir) {
        case 1:poi.setY(poi.y()-1);break;
        case 2:poi.setY(poi.y()+1);break;
        case 3:poi.setX(poi.x()-1);break;
        case 4:poi.setX(poi.x()+1);break;
    }

    int x = poi.x();
    int y = poi.y();
    //如果没撞墙
    if((x<maxWidth)&&(x>=0)&&(y<maxHeight)&&(y>=0))
    {
        //假如吃到了奖励
        if(gridTab[poi.x()][poi.y()]->getState() == 1)
        {
            snakePoi.push_front(poi);
            gridTab[poi.x()][poi.y()]->setState(2);
            for (int index=0;index<award.count();index++) {
                if(award[index] == QPoint(x,y))
                {
                    award.removeAt(index);
                    break;
                }
            }
            //再添加一个奖励
            addAward();
        }
        //一切正常
        else if (gridTab[poi.x()][poi.y()]->getState() == 0)
        {
            snakePoi.push_front(poi);
            gridTab[poi.x()][poi.y()]->setState(2);
            QPoint pop = snakePoi.at(snakePoi.count()-1);
            gridTab[pop.x()][pop.y()]->setState(0);
            snakePoi.pop_back();
        }
        //假如吃到了尾巴
        else {
            start = false;
            killTimer(timerId);
            QMessageBox::information(this,"提示","您的蛇吃到了自己尾巴");
        }
    }
    else {
        start = false;
        killTimer(timerId);
        QMessageBox::information(this,"提示","您的蛇撞墙了");
    }

}

//添加一个奖励
void MainWindow::addAward()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    while(1)
    {
        int x = qrand()%maxWidth;
        int y = qrand()%maxHeight;
        if(gridTab[x][y]->getState() == 0)
        {
            award.append(QPoint(x,y));
            gridTab[x][y]->setState(1);
            break;
        }
    }
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    if(event->timerId() != timerId)
        return;

    if(start == true)
        moveSnake();

}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
        case Qt::Key_W: if(snakeDir!=2)snakeNextDir=1;break;
        case Qt::Key_S: if(snakeDir!=1)snakeNextDir=2;break;
        case Qt::Key_A: if(snakeDir!=4)snakeNextDir=3;break;
        case Qt::Key_D: if(snakeDir!=3)snakeNextDir=4;break;
    }
}

void MainWindow::on_actionStart_triggered()
{
    if(start == true)
        return;

    for (int col=0;col<maxHeight;col++) {
        for (int row=0;row<maxWidth;row++) {
            gridTab[col][row]->setState(0);
        }
    }
    snakePoi.clear();
    addSnack(10,9);
    addSnack(10,10);

    award.clear();
    addAward();
    snakeDir = 1;
    snakeNextDir = 0;
    start = true;
    timerId = startTimer(500);
}



void MainWindow::on_actionStop_triggered()
{
    if(start == false)
        return;
    start = false;
    killTimer(timerId);
}
